//修改密码组件
Vue.component('signed', {
	template: '#signed',
	props:["user_info"],
	data: function() {
		return {	
			host:globalData.constParam.url.baseUrl+"/", //ip
			url: {
				getUserInfo:'/api/UserInfo/GetUserInfo',//获取用户信息
				applyCommonUserVip:'/api/UserInfo/ApplyCommonUserVip', //普通用户签约处理
				applyCompanyUserVip:'/api/UserInfo/ApplyCompanyUserVip', //注册机构
			},
			type:1, //签约类型  1 个人， 2企业
			userinfo:{},//照片信息
		}
	},
	mounted: function() {
		var that = this;
		this.getUserInfo()
		console.log(this.user_info)
		that.user_info.signed_step = 0
	},
	methods: {
		getUserInfo:function(){
			var that = this
			globalData.ajaxPost(this.url.getUserInfo,null,function(res){
				if(res.suc){
					that.userinfo = res.data
					console.log(res.data)
					if(globalData.userInfo.IsVip==1){
						that.type = res.data.UserType
						that.user_info.signed_step = 1
					}else if(globalData.userInfo.IsVip==2){   //未签约
						that.user_info.signed_step = 0
						var arr  =  ['IDCard_Front','IDCard_Back',"IDCard_Body",'IDCard','BusinessLicense','CompanyName','CompanyAddress','CompanyContactor','CompanyContactTel']
						$.each(arr, function(index,item) {
							that.userinfo[item] = null
						});
						
					}
				}
			})
		},
		go_step2:function(index){
			var that = this
			that.user_info.signed_step = 1
			that.type = index
			setTimeout(function(){
				console.log(valid_json)
				
				$('form.signed_form').bootstrapValidator(signed_valid_json);
			},100)
			
		},
		addImg: function(e,key) {
			var that = this;
			var file = e.target.files[0]
			var oFReader = new FileReader();
			oFReader.readAsDataURL(file);
			oFReader.onload = function(oFREvent) {
				var s = oFREvent.target.result;
				var index = s.indexOf(',')
				var t = s.substring(index+1,s.length)
				$(e.target).siblings('img').prop("src", s);
				that.userinfo[key] = t
				
			};
		},
		Submit:function(){
			var that = this;
			console.log($('.signed_form'))
			$('form.signed_form').bootstrapValidator('validate');
			var b = $('form.signed_form').data("bootstrapValidator").isValid();
			if(!b)
				return;
			//提交数据
			console.log(that.userinfo)
			if(!that.userinfo.IDCard_Front || !that.userinfo.IDCard_Body || !that.userinfo.IDCard_Back){
				$('#alert').modal()
				$("#alert p").text("请上传图片")
				return;
			}
			if(that.type==2 && !that.userinfo.BusinessLicense){
				$('#alert').modal()
				$("#alert p").text("请上传营业执照")
				return;
			}
			$('.loading').show()
			var url = that.type==1 ? that.url.applyCommonUserVip : that.url.applyCompanyUserVip
			globalData.ajaxPost(url,that.userinfo,function(res){
				$('.loading').hide()
				if(res.suc){ 
					that.user_info.user.IsVip = 3
					globalData.setStorage(globalData.constParam.param.UTOKEN, that.user_info.user);
					that.getUserInfo();
					
					globalData.getTips("Tip_GivedForVip","",function(res){
						$("#alert").modal()
						$("#alert p").text(res.data);
					});
					
				}else{
					$("#alert").modal()
					$("#alert p").text(res.msg)
				}
			})
			
		}
	}
});

var signed_valid_json = {
	message: 'This value is not valid',
	feedbackIcons: {
		valid: 'glyphicon glyphicon-ok',
		invalid: 'glyphicon glyphicon-remove',
		validating: 'glyphicon glyphicon-refresh'
	},
	fields: {
		IDCard: {
			validators: {
				notEmpty: {
					message: '身份证不能为空'
				}
			}
		},
		CompanyName: {
			validators: {
				notEmpty: {
					message: '机构全称不能为空'
				}
			}
		},
		CompanyAddress: {
			validators: {
				notEmpty: {
					message: '营业地址不能为空'
				}
			}
		},
		CompanyContactor: {
			validators: {
				notEmpty: {
					message: '机构窗口联系人不能为空'
				}
			}
		},
		CompanyContactTel: {
			validators: {
				notEmpty: {
					message: '联系方式不能为空'
				}
			}
		},
	}
}